package com.gitee.cirnochat.user.model.vo.resp;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.gitee.cirnochat.user.model.entity.User;
import com.gitee.cirnochat.user.model.entity.UserDetail;
import com.gitee.cirnochat.user.model.enums.UserStatus;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;


/**
 * 用户信息 VO
 */
@Data
@Schema(name = "用户信息", description = "用户信息")
@AllArgsConstructor
@NoArgsConstructor
public class UserVo {
    @Schema(description = "用户ID", example = "1141514")
    private Long id;

    /**
     * 用户唯一凭证
     */
    @Schema(description = "用户唯一凭证", example = "123e4567174000")
    private String uuid;

    /**
     * 昵称
     */
    @Schema(description = "昵称", example = "John Doe")
    private String nickname;

    /**
     * 头像base64字符串
     */
    @Schema(description = "头像base64字符串", example = "...")
    private String avatar;

    /**
     * 性别：0保密 1男 2女
     */
    @Schema(description = "性别：0保密 1男 2女", example = "1")
    private Integer gender;

    /**
     * 地区
     */
    @Schema(description = "地区", example = "China")
    private String region;

    /**
     * 邮箱
     */
    @Schema(description = "邮箱", example = "bob@gmail.com")
    private String email;


    /**
     * 手机号码
     */
    @Schema(description = "手机号码", example = "+86|12345678901")
    private String mobile;


    /**
     * 出生日期
     */
    @Schema(description = "出生日期", example = "2000-01-01")
    private LocalDate birthday;


    @Schema(description = "用户备注", example = "好友备注")
    private String remark;

    /**
     * 个性签名
     */
    @Schema(description = "个性签名", example = "Hello, world!")
    private String signature;

    /**
     * 是否为好友
     */
    @Schema(description = "是否是好友", example = "true")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Boolean isFriend;

    /**
     * 是否设置了密码
     */
    @Schema(description = "是否设置了密码", example = "true")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Boolean hasPassword;

    /**
     * 用户偏好在线状态
     */
    @Schema(description = "用户偏好在线状态", example = "ONLINE")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private UserStatus preferStatus;

    @Schema(description = "更新时间", example = "2023-10-01T12:00:00")
    private Long createTime;

    @Schema(description = "更新时间", example = "2023-10-01T12:00:00")
    private Long updateTime;


    public UserVo(User user) {
        BeanUtils.copyProperties(user, this);
        this.createTime = user.getCreateTime().toEpochSecond(ZoneOffset.UTC);
        this.updateTime = user.getUpdateTime().toEpochSecond(ZoneOffset.UTC);
        this.hasPassword = user.getPassword() != null && !user.getPassword().isEmpty();
    }

    public UserVo(User user, boolean hiddenPrivateInfo) {
        BeanUtils.copyProperties(user, this);
        this.createTime = user.getCreateTime().toEpochSecond(ZoneOffset.UTC);
        this.updateTime = user.getUpdateTime().toEpochSecond(ZoneOffset.UTC);
        if (user.getNickname() == null) {
            this.nickname = user.getEmail();
        }
        if (hiddenPrivateInfo) {
            this.preferStatus = null;
        } else {
            this.hasPassword = user.getPassword() != null && !user.getPassword().isEmpty();
        }
    }

    public UserVo(UserDetail user) {
        BeanUtils.copyProperties(user, this);
        this.createTime = user.getCreateTime().toEpochSecond(ZoneOffset.UTC);
        this.updateTime = user.getUpdateTime().toEpochSecond(ZoneOffset.UTC);
        if (user.getNickname() == null) {
            this.nickname = user.getEmail();
        }
    }
}
